<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/5/7
 *  Q Q: 505413
 * Time: 11:11
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
class Article extends Base
{

    //*********************************************分类管理*********************************************//
    public function cate()
    {
        $lst = Db::name("article_cate")->order('id asc')->select();
        $this->assign('lst',$lst);
        return $this->fetch();
    }
    //修改分类列表的开启状态
    public function c_status_edit()
    {
        $id = input('param.id');
        $status = Db::name('article_cate')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('article_cate')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('article_cate')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加分类
    public function cateAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');

            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }

            $param['create_time'] = time();
            $param['update_time'] = time();
            $res = Db::name('article_cate')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '添加栏目成功']);
            }else{
                return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '添加栏目失败，请重试']);
            }
        }
        return $this->fetch();
    }
    //编辑分类
    public function cateEdit()
    {
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['update_time'] = time();
            $data = Db::name("article_cate")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'article/index', 'msg' => '修改分类失败']);
            }else{
                return json(['code' => 1, 'url' => 'article/index', 'msg' => '修改分类成功']);
            }
        }
        $cate = Db::name('article_cate')->where(array('id'=>$id))->find();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    //删除分类
    public function cateDel()
    {
        $id = input('id');
        $res = Db::name('article_cate')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除分类成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除分类，请重试']);
        }
    }

    //*********************************************文章管理*********************************************//
    //文章列表
    public function article()
    {
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['title'] = ['like',"%" . $key . "%"];
        }
       $lst = Db::name('article')
           ->alias('a')
           ->field('cxh_article.*,name')
           ->join('article_cate b ','a.cate_id= b.id')
           ->where($map)
           ->paginate(10);
       $this->assign('lst',$lst);
       return $this->fetch();
    }
    //修改文章列表的开启状态
    public function a_status_edit()
    {
        $id = input('param.id');
        $status = Db::name('article')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('article')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('article')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加文章
    public function articleAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['create_time'] = time();
            $param['update_time'] = time();
            unset($param['file']);

            $res = Db::name('article')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '添加文章成功']);
            }else{
                return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '添加文章失败，请重试']);
            }
        }
        $cate = Db::name('article_cate')->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    //缩略图上传
    public function uploadPic()
    {
        $file = request()->file('file');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/thumbnail');//指定上传路径
            $name = $info->getSaveName(); // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $imgSize = $info->getSize();
            $format = array('jpg','png','gif');//定义上传文件类型
            if($imgSize >= '102400'){
                return json(['code' => -2, 'url' => 'article/index', 'msg' => '图片大小不能超过10M']);
            }
            if(false === $info){
                return json(['code' => -1, 'url' => 'article/index', 'msg' => '上传失败，请重试']);
            }else{
                return json(['code' => 1, 'url' => 'article/index', 'msg' => '上传成功','name' => $name]);
            }
        }
    }
    //编辑文章
    public function articleEdit()
    {
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['update_time'] = time();
            unset($param['file']);
            $data = Db::name("article")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'link/index', 'msg' => '修改文章失败']);
            }else{
                return json(['code' => 1, 'url' => 'link/index', 'msg' => '修改文章成功']);
            }
        }
        $article = Db::name('article')->where(array('id'=>$id))->find();
        $cate = Db::name('article_cate')->select();
        $this->assign('cate',$cate);
        $this->assign('article',$article);
        return $this->fetch();
    }
    //删除文章
    public function articleDel()
    {
        $id = input('id');
        $res = Db::name('article')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除文章成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除文章失败，请重试']);
        }
    }
}